package com.runner.unlocks;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.runner.observer.Event;
import com.runner.observer.Subject;
import com.runner.utility.Constants;
import com.runner.utility.Locator;

public class AprilUnlock implements Constants {

	public static final int APRIL_AMOUNT = 30;
	private boolean unlocked;
	private ImageButton button;
	private Subject subject;
	private Label unlockField, name;
	private Stage stage;
	private Image coinImage;

	public AprilUnlock(Stage stage, LabelStyle style, Subject subject) {
		this.subject = subject;
		this.stage = stage;
		unlocked = Locator.getUnlocks().isApril();
		createButton(style);
	}

	public void createButton(LabelStyle style) {
		Image i = new Image(Locator.getAssets().get(APRIL_PICTURE, Texture.class));
		button = new ImageButton(i.getDrawable());
		button.addListener(new ClickListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				subject.notify(Event.EVENT_APRIL_SELECTED);
				return false;
			}
		});
		button.setPosition(ACTUAL_VIRTUAL_WIDTH * (RINGER_X + SEPARATION_X), ACTUAL_VIRTUAL_HEIGHT * (RINGER_Y+ HEIGHT_ADJUST));
		button.getImage().setScale(1 );
		button.setScale(1 );
		stage.addActor(button);

		unlockField = new Label("Unlock " + APRIL_AMOUNT, style);
		unlockField.setPosition(ACTUAL_VIRTUAL_WIDTH * (RINGER_X + SEPARATION_X + 0.01f), ACTUAL_VIRTUAL_HEIGHT * (RINGER_Y + 0.01f+ HEIGHT_ADJUST));

		name = new Label("April", style);
		name.setPosition(ACTUAL_VIRTUAL_WIDTH * (RINGER_X + SEPARATION_X + 0.045f), ACTUAL_VIRTUAL_HEIGHT * (RINGER_Y + 0.168f+ HEIGHT_ADJUST));
		stage.addActor(name);

		coinImage = new Image(Locator.getAssets().get(SINGLE_COIN_PATH, Texture.class));
		coinImage.setPosition(ACTUAL_VIRTUAL_WIDTH * (RINGER_X + SEPARATION_X + 0.10f), ACTUAL_VIRTUAL_HEIGHT * (RINGER_Y + 0.015f+ HEIGHT_ADJUST));
		coinImage.setScale((1 ) * 0.57F);

		if (!unlocked) {
			stage.addActor(unlockField);
			stage.addActor(coinImage);
		}
	}

	public boolean isUnlocked() {
		return unlocked;
	}

	public boolean unlock() {
		if (Locator.getCoins() >= APRIL_AMOUNT) {
			unlocked = true;
			unlockField.remove();
			coinImage.remove();
			Locator.removeCoins(APRIL_AMOUNT);
			Locator.getUnlocks().setApril(true);
			Locator.writeSave();
			subject.notify(Event.EVENT_APRIL_UNLOCKED);
			subject.notify(Event.EVENT_APRIL_SELECTED);
			return true;
		}
		return false;
	}

	public PLAYER_TYPE getType() {
		return PLAYER_TYPE.APRIL;
	}

}
